package site.kexing.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import site.kexing.mapper.EquipmentMapper;
import site.kexing.mapper.Equipment_ProductMapper;
import site.kexing.mapper.ProductMapper;
import site.kexing.pojo.Equipment_Product;
import java.util.List;

@Controller
public class Equipment_ProductController {
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private Equipment_ProductMapper equipment_productMapper;

    @RequestMapping("/selectallequipmentproduct")
    public String selectAllEquipment_Product(@RequestParam(defaultValue = "1") Integer currentPage, Model model){
        PageHelper.startPage(currentPage, 7);
        List<Equipment_Product> equipment_products = equipment_productMapper.selectAllEquipment_Product();
        PageInfo<Equipment_Product> pageInfo = new PageInfo<Equipment_Product>(equipment_products,7);
        model.addAttribute("pageInfo",pageInfo);
        return "equipment_product";
    }


    @RequestMapping("/selectequipmentproductbyid")
    public String selectEquipment_ProductByID(Integer id, Model model){
        //查询所有的设备ID
        List<Integer> equipmentIds = equipmentMapper.selectAllEquipmentID();
        //查询所有的产品ID
        List<Integer> productIds = productMapper.selectAllProductID();
        Equipment_Product equipment_product = equipment_productMapper.selectEquipment_ProductByID(id);
        model.addAttribute("equipment_product",equipment_product);
        model.addAttribute("equipmentIds",equipmentIds);
        model.addAttribute("productIds",productIds);
        return "equipment_productUpdate";
    }

    @RequestMapping("/updateequipmentproduct")
    public String updateEquipment_Product(Equipment_Product equipment_product){
        int i = equipment_productMapper.updateEquipment_Product(equipment_product);
        return "redirect:/selectallequipmentproduct";
    }

    @RequestMapping("/deleteequipmentproduct")
    public String deleteEquipment_Product(Integer id){
        int i = equipment_productMapper.deleteEquipment_Product(id);
        return "redirect:/selectallequipmentproduct";
    }

    @RequestMapping("/equipment_productInsert")
    public String toEquipment_productInsert(Model model){
        //查询所有的设备ID
        List<Integer> equipmentIds = equipmentMapper.selectAllEquipmentID();
        //查询所有的产品ID
        List<Integer> productIds = productMapper.selectAllProductID();
        model.addAttribute("equipmentIds",equipmentIds);
        model.addAttribute("productIds",productIds);
        return "equipment_productInsert";
    }

    @RequestMapping("/insertequipmentproduct")
    public String insertEquipment_Product(Equipment_Product equipment_product,Model model){
        int i = equipment_productMapper.insertEquipment_Product(equipment_product);
        return "redirect:/selectallequipmentproduct";
    }

    @RequestMapping("/selectequipmentproductbyname")
    public String selectEquipment_ProductByName(@RequestParam(defaultValue = "1") Integer currentPage,String equipment_name,Model model){
        PageHelper.startPage(currentPage, 7);
        List<Equipment_Product> equipment_products = equipment_productMapper.selectEquipment_ProductByName(equipment_name);
        PageInfo<Equipment_Product> pageInfo = new PageInfo<Equipment_Product>(equipment_products,7);
        model.addAttribute("pageInfo",pageInfo);
        return "equipment";
    }
}
